package com.winit.project.web;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.base.beans.LoginUser;
import com.winit.common.base.web.BaseController;
import com.winit.common.filter.UserInfoUtil;
import com.winit.common.util.FileUtil;
import com.winit.common.util.LogUtil;
import com.winit.project.core.build.ProjectBuilder;
import com.winit.project.manager.ProjectManager;
import com.winit.project.util.ProjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by keel2008 on 16/9/27.
 */

@RestController(value = "newBuildController")
@RequestMapping("/project")
public class BuildController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(BuildController.class);

    @Autowired
    private ProjectManager projectManager;

    /**
     * 执行构建
     * @param request
     * @return
     */
    @RequestMapping("/doBuild")
    @ResponseBody
    public Map<String,Object> doBuild(HttpServletRequest request){
        Map<String,Object> rtnMap = new HashMap<>();
        logger.info("执行构建开始");
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null){
            logger.info("执行构建,获取用户信息为空");
            return this.noUserRtn();
        }

        String projectName = request.getParameter("projectName");
        Assert.hasText(projectName,"执行构建,工程名为空");

        try {
            projectManager.doBuild(projectName,userInfo);
            rtnMap.put("code","0");
        }catch (Exception e){
            logger.error("执行构建异常,projectName="+projectName,e);
            rtnMap.put("code","1");
            rtnMap.put("desc","执行构建异常,"+e.getMessage());
        }
        return this.getRtnMap(rtnMap,userInfo,new JSONObject());
    }

    /**
     * 获取工程构建日志
     * @param request
     * @return
     */
    @RequestMapping("/getBuildLog")
    @ResponseBody
    public Map<String,Object> getBuildLog(HttpServletRequest request){
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null) {
            logger.info("获取工程构建日志,获取用户信息为空");
            return noUserRtn();
        }
        Map<String,Object> rtnMap = new HashMap<>();
        String projectName = request.getParameter("projectName");

        Assert.hasText(projectName,"获取工程构建日志,工程名称不能为空");
        JSONObject data = new JSONObject();
        String logPath = null;
        try {
            ProjectBuilder builder = new ProjectBuilder(projectName,null);
            logPath = builder.getBuildLogFilePath();
            String log = FileUtil.getFileByLine(logPath,0);
            rtnMap.put("code","0");
            data.put("log",log);
            data.put("logPath",logPath);
        } catch (Exception e) {
            logger.error("获取工程构建日志异常,projectName="+projectName,e);
            rtnMap.put("code","1");
            rtnMap.put("desc","获取工程构建日志异常,e:"+e.getMessage());
            data.put("logPath",logPath);
        }
        return getRtnMap(rtnMap,userInfo,data);
    }
}
